//
//  ActivityDetailViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/9.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "UserLandViewController.h"
#import "DateBaseSingleton.h"
#import "ActivityDetailList.h"
@interface ActivityDetailViewController ()

@end

@implementation ActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    
    
    UIScrollView *activitySc = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    
    
   
    
    
    
    self.activityView = [[ActivityDetailView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.activityView.backgroundColor = [UIColor whiteColor];
    self.activityView.actList = self.activity;
    [activitySc addSubview:self.activityView];
    
    [self.activityView getHeightblock:^(CGFloat height) {
       
        self.scHeight = height;
        
    }];
    
    
    activitySc.contentSize = CGSizeMake(375, self.scHeight + 360 );
    
    
    [self.view addSubview:activitySc];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    
    
    
    // Do any additional setup after loading the view.
}

/*****************************************************************/

/*收藏逻辑
 1.插入之前先判断该电影是否已经收藏过
 2.取出NSUserDefaults保存的登录用户名
 3.网络数据库里插入收藏信息
 
 
 */


- (void)buttonAction:(UIBarButtonItem *)btn{
    
    NSString *login = [[NSUserDefaults standardUserDefaults]objectForKey:@"isLogin"];
    
    if ([login isEqualToString:@"1"]) {
        
        
        
        [[DateBaseSingleton shareDateBase]creatActivityDetailTable];
        
        self.activity.userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        
        
        
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[[DateBaseSingleton shareDateBase]selectActivityDetailName:self.activity.userName]];
          if ([array containsObject:self.activity.title]) {
                
        


                
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已经被收藏" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                }];
                [alertC addAction:action];
                
                
            }

        else
            {
                
                
                [[DateBaseSingleton shareDateBase]insertActivityDeatail:self.activity];//插入
                
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                }];
                [alertC addAction:action];
                
        
                
                
            }
    
    
    }else
            {
                
                
                UserLandViewController *userLandVC = [[UserLandViewController alloc]init];
                
                [self.navigationController pushViewController:userLandVC animated:YES];
                
                
            }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
